package com.woniuxy.mqproducer.cofiguration;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * description: CallbackCofiguration <br>
 * date: 2022/1/12 10:52 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 定义配置类，实现接口，启动生产者确认
 */
@Configuration
public class CallbackCofiguration implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //  @PostConstruct   @PreDestroy
    //SpringBean的声明周期<bean id="" class="" init-method="" destroy-method=""/>

    //构造器 属性赋值 setUp(init)	执行主业务方法	tearDown(destroy)
    //其他声明周期方法都需要实现特定的接口

    @PostConstruct
    public void setUp() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {

        System.out.println("confirm");
        System.out.println("correlationData:" + correlationData);
        System.out.println("ack:" + ack);
        System.out.println("cause:" + cause);
        System.out.println("------------------");
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText,
                                String exchange, String routingKey) {

        System.out.println("return");
        System.out.println("message" + message);
        System.out.println("replyText:" + replyText);
        System.out.println("exchange:" + exchange);
        System.out.println("routingKey:" + routingKey);
        System.out.println("------------------");

    }
}
